#include "chooselevelscene.h"
#include <QMenuBar>
#include <QAction>
#include <QPainter>
#include "mybutton.h"
#include <QTimer>
#include "QLabel"
#include <QSoundEffect>
chooselevelscene::chooselevelscene(QWidget *parent)
    : QMainWindow{parent}
{
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("选择关卡");
    QMenuBar *bar=this->menuBar();
    this->setMenuBar(bar);
    QMenu *startMenu=bar->addMenu("开始");
    QAction *quitAction=startMenu->addAction("退出");

    QSoundEffect *startsound = new QSoundEffect(this);

    QSoundEffect *backsound = new QSoundEffect(this);



    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    mybutton *closebtn=new mybutton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    closebtn->setParent(this);
    closebtn->move(this->width()-closebtn->width(),this->height()-closebtn->height());
    connect(closebtn,&mybutton::clicked,[=](){
        backsound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
        backsound->play(); //开始音效
        QTimer::singleShot(500, this,[=](){
            this->hide();
            //触发自定义信号，关闭自身，该信号写到 signals下做声明
            emit this->chooseSceneBack();
        }
                           );
    });
    for(int i = 0 ; i < 20;i++)
    {
        mybutton * menuBtn = new mybutton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25 + (i%4)*70 , 130+ (i/4)*70);

        //按钮上显示的文字
        QLabel * label = new QLabel;
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); //设置居中
        label->move(25 + (i%4)*70 , 130+ (i/4)*70);
        label->setAttribute(Qt::WA_TransparentForMouseEvents,true);  //鼠标事件穿透
        connect(menuBtn,&mybutton::clicked,[=](){
            startsound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
            startsound->play(); //开始音效
            if(pscene==NULL){
                this->hide();
                pscene=new playscene(i+1);
                pscene->show();
                connect(pscene,&playscene::chooseSceneBack,[=](){
                    this->show();
                    delete pscene;
                    pscene = NULL;
                });
            }
        });
    }

}
void chooselevelscene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,20,pix.width(),pix.height(),pix);

}
// void chooselevelscene::paintEvent(QPaintEvent *){
//     QPainter painter(this);
//     QPixmap pix;
//     pix.load(":/res/PlayLevelSceneBg.png");
//     painter.drawPixmap(0,0,this->width(),this->height(),pix);
//     pix.load(":/res/Title.png");
//     pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
//     painter.drawPixmap(10,30,pix.width(),pix.height(),pix);

// }
